package model.event;

import view.layers.Layer;
import view.layers.UserLayer;

/**
 * 
 *
 * @author rhaq
 * @version 1.00 2009-05-11 Initial submission.
 */
public class RandomEventGenerator implements Runnable
{
	/** Maximum amount of time to pass until another event can be generated. */
	private static final long MAX_DELAY = 5000;

	/** Minimum amount of time to pass until another event can be generated. */
	private static final long MIN_DELAY = 1000;

	/** */
	private EventGenerator generator;

	/** Is there a game in progress? */
	private Boolean playing;

	/** */
	private RandomGenerator random;


	/**
	 * 
	 */
	public RandomEventGenerator(Boolean playing, UserLayer eventLayer, Layer userLayer)
	{
		generator = new EventGenerator(userLayer, eventLayer);
		random = RandomGenerator.getInstance();
		this.playing = playing;
	}


	/* (non-Javadoc)
	 * @see java.lang.Runnable#run()
	 */
	public void run()
	{
		try {

			while ( playing.booleanValue() )
			{
				Event e = null;
				int randNum = random.nextInt(0, 1);
				System.out.println("rand num = "+randNum);

				switch (randNum)
				{
					case 0:
						System.out.println("0");
						e = generator.generatePoliceChase();
						break;
					case 1:
						System.out.println("1");
						e = generator.generateTheft();
						break;
					case 2:
						System.out.println("2");
						e = generator.generateTheft();
						break;
					case 3:
						System.out.println("3");
						e = generator.generateUnderCoverBust();
						break;
					default:
						e = generator.generateTheft();
						break;
				}

				e.process();

				long sleepTime = random.nextLong(MIN_DELAY, MAX_DELAY);
				Thread.sleep(sleepTime);
			}
		}

		catch (Exception ex)
		{
			ex.printStackTrace();
		}
	}
}